﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Top.Api.Domain;
using Top.Api.Request;
using Top.Api.Response;
using Top.Api;
using DBControl;
using System.Data;

namespace KSGJ
{
    public partial class robot : validationclass
    {
        private static string appkey = System.Configuration.ConfigurationManager.AppSettings["AppKey"].ToString();
        private static string appsecret = System.Configuration.ConfigurationManager.AppSettings["AppSecret"].ToString();
        private static string appurl = System.Configuration.ConfigurationManager.AppSettings["AppUrl"].ToString();
        private static ITopClient client = new DefaultTopClient(appurl, appkey, appsecret);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["subId"] != null)
                {
                    this.notuse.Visible = true;
                    this.notusePanel.Visible = true;
                    this.subpanel.Visible = false;
                    this.controlpanel.Visible = false;
                }
                else
                {
                    getsubuser();
                }
            }
        }

        private void getsubuser()
        {
            SellercenterSubusersGetRequest ssgr = new SellercenterSubusersGetRequest();
            ssgr.Nick = Session["usNick"].ToString();
            SellercenterSubusersGetResponse ssgrp = client.Execute(ssgr, Session["sessionKey"].ToString());
            if (!ssgrp.IsError)
            {
                //this.userlist.DataSource = ssgrp.Subusers.FindAll(delegate(SubUserInfo subinfo) { return subinfo.Status == 1; });
                this.userlist.DataSource = ssgrp.Subusers;
                this.userlist.DataBind();
            }
        }

        protected void userlist_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                SqlDbHelper sdh = new SqlDbHelper();
                SubUserInfo userinfo = e.Item.DataItem as SubUserInfo;
                string getsubuser = "SELECT * FROM UserSession WHERE UsID = '" + userinfo.SubId + "'";
                DataTable dt = sdh.ExecuteDataTable(getsubuser);
                if (dt.Rows.Count > 0)
                {
                    TextBox mt = e.Item.FindControl("maxmoney") as TextBox;
                    //mt.Text = String.Format("0:N2",Decimal.Parse(dt.Rows[0]["maxmoney"].ToString()));
                    mt.Text = String.Format("{0:F2}", Decimal.Parse(dt.Rows[0]["maxmoney"].ToString())); 
                    string isp = dt.Rows[0]["IsAllPromotion"].ToString();
                    System.Web.UI.HtmlControls.HtmlInputCheckBox cbx = e.Item.FindControl("subcheckbox") as System.Web.UI.HtmlControls.HtmlInputCheckBox;
                    cbx.Checked = true;
                    System.Web.UI.HtmlControls.HtmlGenericControl cblabel = e.Item.FindControl("sublabel") as System.Web.UI.HtmlControls.HtmlGenericControl;
                    cblabel.Attributes["class"] = "checkbox checkbox-checked";
                    TextBox max = e.Item.FindControl("maxmoney") as TextBox;
                    max.Enabled = true;
                    if (isp.Equals("0"))
                    {
                        System.Web.UI.HtmlControls.HtmlInputRadioButton rb = e.Item.FindControl("radio1") as System.Web.UI.HtmlControls.HtmlInputRadioButton;
                        System.Web.UI.HtmlControls.HtmlGenericControl lb1 = e.Item.FindControl("radiolabel1") as System.Web.UI.HtmlControls.HtmlGenericControl;
                        lb1.Attributes["class"] = "radio-button radio-button-checked";
                        System.Web.UI.HtmlControls.HtmlGenericControl lb2 = e.Item.FindControl("radiolabel2") as System.Web.UI.HtmlControls.HtmlGenericControl;
                        lb2.Attributes["class"] = "radio-button";
                        rb.Checked = true;
                    }
                    else
                    {
                        System.Web.UI.HtmlControls.HtmlInputRadioButton rb = e.Item.FindControl("radio2") as System.Web.UI.HtmlControls.HtmlInputRadioButton;
                        rb.Checked = true;
                    }
                }
                else
                {
                    System.Web.UI.HtmlControls.HtmlInputRadioButton rb = e.Item.FindControl("radio2") as System.Web.UI.HtmlControls.HtmlInputRadioButton;
                    rb.Checked = true;
                    System.Web.UI.HtmlControls.HtmlGenericControl lb2 = e.Item.FindControl("radiolabel2") as System.Web.UI.HtmlControls.HtmlGenericControl;
                    lb2.Attributes["class"] = "radio-button radio-button-checked";
                }
            }
            
        }
    }
}